package com.web.exiaodao.remote.controller;
import java.util.Map;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.web.webstart.base.util.jpa.WebUitl;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

import com.web.webstart.base.constant.XaConstant;
import com.web.webstart.base.controller.BaseController;
import com.web.webstart.base.exception.BusinessException;
import com.web.webstart.base.util.result.XaResult;
import com.web.exiaodao.remote.service.ApiLotteryRuleService;
import com.web.exiaodao.remote.vo.LotteryRuleVo;
import com.web.exiaodao.business.entity.LotteryRule;

/**
 * @Title: ApiLotteryRuleController.java
 * @Package com.web.exiaodao.remote.controller
 * @Description: 签到-超级转一转规则信息接口
 * @author zhanglin
 * @date 2015年3月23日 下午1:00:00
 * @version V1.0
 */
@Api(value = "LotteryRule", description = "签到-超级转一转规则接口", position = 10)
@Controller
@RequestMapping("/api/lotteryRule")
public class ApiLotteryRuleController extends BaseController {

	private static final Logger LOGGER = Logger.getLogger(ApiLotteryRuleController.class);

	@Autowired
	private ApiLotteryRuleService lotteryRuleService;
	
	/**
	 * @Title: findLotteryRuleList
	 * @Description: 查询所有签到-超级转一转规则信息
	 * @return    
	 */
	@ApiOperation(value="查询所有签到-超级转一转规则",notes="查询所有签到-超级转一转规则信息,当返回的code=1时，取出object进行显示,存放为数组对象")
	@ResponseBody
	@RequestMapping(value="findLotteryRuleList",method=RequestMethod.POST)
	public XaResult<List<LotteryRuleVo>> findLotteryRuleList(
		@ApiParam("页号,字段名:nextPage,默认0,从第0页开始") @RequestParam(defaultValue = "0") Integer nextPage,
		@ApiParam("页长,字段名:pageSize,默认20") @RequestParam(defaultValue = "20") Integer pageSize
	) throws BusinessException{
		Pageable pageable = WebUitl.buildPageRequest(nextPage, pageSize, "[{property:'createTime',direction:'DESC'}]");
		Map<String , Object> filterParams =  WebUitl.getParametersStartingWith("{}", "search_");
		return lotteryRuleService.findListEQStatusByFilter(XaConstant.Status.valid, filterParams, pageable);
	}

	
	/**
	 * @Title: findLotteryRuleById
	 * @Description: 根据ID查找单条实体
	 * //@param modelId
	 * @return    
	 */
	@ApiOperation(value="签到-超级转一转规则详情",notes="查询签到-超级转一转规则详细信息,当返回的code=1时，取出object进行显示,存放为单体对象")
	@ResponseBody
	@RequestMapping(value="findLotteryRuleById",method=RequestMethod.POST)
	public XaResult<LotteryRuleVo> findLotteryRuleById(
		@ApiParam("编号,字段名:id,必填") @RequestParam(value = "id") Long id
	) throws BusinessException{
		XaResult<LotteryRuleVo> xr = new XaResult<LotteryRuleVo>();
		try{
			return lotteryRuleService.findOne(id);
		}catch(Exception e){
			xr.error(XaConstant.Message.error);
			LOGGER.error(XaConstant.Message.error,new Throwable(e));
		}
		return xr;
	}
	
	/**
	 * @Title: save
	 * @Description: 新增一条实体
	 * //@param userId
	 * //@param model
	 * @return    
	 */
	@ApiOperation(value="新增签到-超级转一转规则",notes="供前端前期填充数据测试使用,上线后删除;新增签到-超级转一转规则,当返回的code=1时，保存成功后object返回对象")
	@ResponseBody
	@RequestMapping(value="saveLotteryRule",method=RequestMethod.POST)
	public XaResult<LotteryRuleVo> saveLotteryRule(
		@ApiParam("抽到的亲和币,字段名:money") @RequestParam(value = "money") Long money,
		@ApiParam("奖项备注,字段名:memo") @RequestParam(value = "memo") String memo,
		HttpServletRequest request
	) throws BusinessException{
		XaResult<LotteryRuleVo> xr = new XaResult<LotteryRuleVo>();
		try{
		LotteryRule model = new LotteryRule();
		model.setMoney(money);
		model.setMemo(memo);
		return lotteryRuleService.createModel(model);
		}catch(Exception e){
			xr.error(XaConstant.Message.error);
			LOGGER.error(XaConstant.Message.error,new Throwable(e));
		}
		return xr;
	}
	
	/**
	 * @Title: 
	 * @Description: 修改一条实体
	 * //@param userId
	 * //@param model
	 * @return    
	 */
	@ApiOperation(value="修改签到-超级转一转规则",notes="修改签到-超级转一转规则,当返回的code=1时，保存成功后object返回对象")
	@ResponseBody
	@RequestMapping(value="updateLotteryRule",method=RequestMethod.POST)
	public XaResult<LotteryRuleVo> updateLotteryRule(
		@ApiParam("抽到的亲和币,字段名:money") @RequestParam(value = "money") Long money,
		@ApiParam("奖项备注,字段名:memo") @RequestParam(value = "memo") String memo,
		@ApiParam("版本编号,字段名:tId") @RequestParam(value = "tId") Long tId,
	HttpServletRequest request
	) throws BusinessException{
		XaResult<LotteryRuleVo> xr = new XaResult<LotteryRuleVo>();
		try{
		LotteryRule model = new LotteryRule();
		model.setMoney(money);
		model.setMemo(memo);
		model.setId(tId);
		return lotteryRuleService.updateModel(model);
		}catch(Exception e){
			xr.error(XaConstant.Message.error);
			LOGGER.error(XaConstant.Message.error,new Throwable(e));
		}
		return xr;
	}
	
	/**
	 * @Title: operateLotteryRuleById
	 * @Description: 操作一个实体状态,根据status进行操作
	 * @param modelIds
	 * @param status 		操作类型:-1锁定,0无效,1正常,2发布,3删除,默认删除操作
	 * @return    
	 */
	@ApiOperation(value="根据ID修改签到-超级转一转规则状态",notes="修改签到-超级转一转规则状态,当返回的code=1时，保存成功后object返回对象")
	@ResponseBody
	@RequestMapping(value="operateLotteryRuleById",method=RequestMethod.POST)
	public XaResult<LotteryRuleVo> operateLotteryRuleById(
		HttpServletRequest request,
		@ApiParam("编号,字段名:modelIds,必填") @RequestParam(value = "modelId") String modelIds,
		@ApiParam("操作类型,字段名:status,-1锁定,0无效,1正常,2发布,3删除,选填,默认删除操作") @RequestParam(defaultValue = "3") Integer status
	) throws BusinessException{
		XaResult<LotteryRuleVo> xr = new XaResult<LotteryRuleVo>();
		try{
			return lotteryRuleService.multiOperate(modelIds,status);
		}catch(Exception e){
			xr.error(XaConstant.Message.error);
			LOGGER.error(XaConstant.Message.error,new Throwable(e));
		}
		return xr;
	}
}

